/*
 * @Description:配置组件
 * @Author: hyx
 * @Date: 2021-03-19 11:35
 */
// 选择配置
import { defineComponent, PropType } from 'vue'
import { options } from '@/dictionaries/config'
// import { isTrue } from 'rantion-tools/es'
const Props = {
	value: [String, Number] as PropType<string | number>,
	label: [String, Number] as PropType<string | number>,
	// @Prop(String) readonly prop!: string,
	prop: {
		type: String as PropType<string>,
		default: 'status',
		required: true,
	},
	disabled: {
		type: Boolean as PropType<boolean>,
		default: false,
	},
	mode: String as PropType<string>,
	placeholder: String as PropType<string>,
	allowClear: {
		type: Boolean as PropType<boolean>,
		default: true,
	},
} as const
const FormConfig = defineComponent({
	name: 'form-config',
	props: Props,
	emits: {
		change: Function as unknown as (value: number) => void,
		'update:value': Function as (value?: string) => void,
		'update:label': Function as (value?: string) => void,
	},
	setup(props, { emit }) {
		function onChange(value: number, option?: any) {
			if (option) {
				emit('update:value', option.value)
				emit('update:label', option.label)
			} else {
				emit('update:value', undefined)
				emit('update:label', undefined)
			}
			emit('change', value)
		}

		return () => {
			// const def = isTrue(porps.value) ? porps.value + '' : porps.value

			const def = isNaN(Number(props.value)) ? props.value : Number(props.value)

			return (
				<a-select
					onChange={onChange}
					mode={props.mode}
					disabled={props.disabled}
					allowClear={props.allowClear}
					value={def}
					placeholder={props.placeholder}
					options={options[props.prop]}
				/>
			)
		}
	},
})

export default FormConfig
